Welcome

R Markdown

Aaron Williams

IBP and Program on Retirement Policy

rmarkdown

Stop, Disclaimer, Warning:

stop

Before starting any public-facing work, please contact:

Via Roulex_45

R Markdown

  • Code
  • Code Output
  • Narrative

Example

We used a linear model because…

model1 <- lm(formula = dist ~ speed, data = cars)
model1
## 
## Call:
## lm(formula = dist ~ speed, data = cars)
## 
## Coefficients:
## (Intercept)        speed  
##     -17.579        3.932

An increase in travel speed of one mile per hour is associated with a 3.93 foot increase in stopping distance.

Abundance of Outputs

knitr Language Engines

  • Python
  • SQL
  • Bash
  • Rcpp
  • Stan
  • Javascript
  • CSS

Motivation

Problem #1: Copying-and-Pasting

Copy-and-Paste

Copying-and-pasting tables, charts, and numbers is tedious, error-prone, and labor intensive.

Problem #1.5: Repetition

Copy-and-Paste

Iterating entire documents by geography, time period, or set of assumptions is even more tedious, error-prone, and labor intensive.

Problem #2: Out-of-sequence

out-of-sync

  • Run parts of a script out of order.
  • Change code without changing tables and figures.

Via die-seite-des-dr-caligari

Problem #3: Parallel Documents

parallel

  • Lab notebook
  • Reports that are more interesting than can be captured by paper

Via die-seite-des-dr-caligari

Problem #4: Is code written for machines or humans?

computer

Via JoeDaEskimo

Literate (Statistical) Programming

Don Knuth

knuth

Let us change our traditional attitude to the construction of programs: Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do. ~Literate Programming (1984)

Via Jacob Applebaum

Building a Document

R markdown

Flow

R for Data Science by Hadley Wickham and Garrett Grolemund

Three Ingredients for R Markdown

  • YAML Header
  • Markdown Text
  • Code Chunks

YAML Header

YAML Header

Yet Another Markup Language

Document type and settings

YAML Header

---
title: "R Markdown"
author: "Aaron Williams"
date: "June 22, 2017"
output: html_document
---  

Options

---
title: "R Markdown"
author: "Aaron Williams"
date: "June 22, 2017"
output: 
  html_document:
    code_folding: hide
    toc: true
    toc_float: TRUE 
---  

NOTE: Use spaces, not tabs!

Example

Markdown

Shortcut for HTML

HyperText Markup Language

<h2>Title</h2>
  
<h3>Subtitle</h3>  
  
<a href="www.rstudio.com">text</a>

<ul>
  <li>Bulleted List</li>
</ul>

Markdown

Pandoc Markdown

Pandoc Markdown

TeX Equations

  • $E = mc^2$

\(E = mc^2\)

  • $y = \mu + \sum_{i=1}^p \beta_ix_i + \epsilon$

\(y = \mu + \sum_{i=1}^p \beta_i x_i + \epsilon\)

Pandoc Markdown

Bibliography

---
title: "Bibliography Example"
output: html_document
bibliography: bibliography.bib
biblio-style: "apalike"
link-citations: true
---

Pandoc Markdown

Bibliography

@Xie{R-base,
  title = {Authoring Books with R Markdown},
  author = {{Yihui Xie}},
  organization = {R Studio},
  address = {Nebraska},
  year = {2016},
  url = {https://bookdown.org/yihui/bookdown/citations.html},
}

Pandoc Markdown

Bibliography

  • Blah blah [see @Xie, pp. 33-35].

  • Blah Yuihi Xie blah [see -@Xie, pp. 33-35].

Example

Code Chunks

Template

\ ```{}
\ ```

Template

\ ```{<language> <codechunk name>}
\ ```
  • Names should be unique
  • Name chunks after what they create
  • Names are useful for navigation
  • Names are important for caching

Earlier Example

We used a linear model because…

model1 <- lm(formula = dist ~ speed, data = cars)
model1
## 
## Call:
## lm(formula = dist ~ speed, data = cars)
## 
## Coefficients:
## (Intercept)        speed  
##     -17.579        3.932

An increase in travel speed of one mile per hour is associated with a 3.93 foot increase in stopping distance.

Earlier Example

We used a linear model because…

\```{r linear model}
model1 <- lm(formula = dist ~ speed, data = cars)
model1
\```
## 
## Call:
## lm(formula = dist ~ speed, data = cars)
## 
## Coefficients:
## (Intercept)        speed  
##     -17.579        3.932

An increase in travel speed of one mile per hour is associated with a 3.93 foot increase in stopping distance.

`r round(model1$coefficients[2], 2)`

Demo!

Other Types of Output

html_notebook

html_notebook - Interactive R Notebooks - Data scientists/researchers lab notebook

Websites

html widget

This Presentation!

Books

Bookdown

More!

Parameters

Iterate

library(fivethirtyeight)
library(stringr)

# Create vector of state names
state <- bad_drivers$state

#Create data frame with parameters
reports <- tibble(
    filename = str_c("driving-", state, ".html"),
    params = map(state, ~list(state = .))
)

# Iterate across parameters and knit documents for 51 states
reports %>%
    select(output_file = filename, params) %>%
    pwalk(rmarkdown::render, input = "demonstration.Rmd")

Closing

R Markdown vs. LaTeX

LaTeX

R Markdown and LaTeX are compliments, not substitutes.

  • R Markdown is way easier than LaTeX
  • R Markdown creates more output types than LaTeX
  • LaTeX has more control and is better for documents with page breaks

Via Thenub314

More Resources

Questions and Comments?